package com.kaikeba.controller;

import com.kaikeba.bean.BootStrapTableExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.PageData;
import com.kaikeba.exception.DuplicateCodeException;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.DruidUtil;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> console = ExpressService.console();
        Message msg = new Message();
        if(console.size() == 0){
            msg.setStatus(-1);
        }else{
            msg.setStatus(0);
        }
        msg.setData(console);
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1、 获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        // 2、  获取当前页需要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        // 3、进行查询
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for(Express e : list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未取件":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==1?"已取件":"未取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            System.out.println(e2.toString());
            list2.add(e2);
        }
        List<Map<String, Integer>> console = ExpressService.console();
        Integer data1_size = console.get(0).get("data1_size");
        // 4、 将集合封装为 bootstap-table识别的格式
        PageData pageData = new PageData();
        pageData.setRows(list2);
        pageData.setTotal(data1_size);
        String json = JSONUtil.toJSON(pageData);
        System.out.println(json);
        return json;
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest request,HttpServletResponse response) throws DuplicateCodeException {
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String userPhone = request.getParameter("userPhone");
        String username = request.getParameter("username");
        HttpSession session = request.getSession();
        Express e = new Express(number,username,userPhone,company, UserUtil.getUserPhone(session));
        boolean insert = ExpressService.insert(e);
        Message msg = new Message();
        if(insert){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递录入成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("快递录入失败");
        }
        String json = JSONUtil.toJSON(msg);

        return json;
    }


    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String number = request.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        Message msg = new Message();
        if(e == null){
           msg.setStatus(-1);
           msg.setResult("单号不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        int status = Integer.parseInt(request.getParameter("status"));
        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUserName(username);
        newExpress.setUserPhone(userPhone);
        newExpress.setStatus(status);
        boolean flag = ExpressService.update(id, newExpress);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setData("更改快递成功");
        }else {
            msg.setStatus(-1);
            msg.setData("数据更改失败");
        }
        String s = JSONUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = ExpressService.delete(id);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除快递成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除快递失败");
        }
        String s = JSONUtil.toJSON(msg);
        return s;
    }
}
